package br.comdecorando.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import br.com.decorando.activity.R;
import br.com.decorando.model.Carta;
import br.com.decorando.model.Perfil;



public class CartaAdapter extends ArrayAdapter<Carta> {
	private List<Carta> itens;
	private int layoutResourceId;
	private Context context;	
	private CartaHolder editHolder;
	
	public CartaAdapter(Context context, int layoutResourceId, List<Carta> itens) {
		super(context, layoutResourceId, itens);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.itens = itens;
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CartaHolder holder = new CartaHolder();
		holder.carta = itens.get(position);		
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		View row = inflater.inflate(layoutResourceId, parent, false);
		setComponents(holder, row);		
		if(holder.equals(editHolder)){
			startEdit(holder);			
		}		
		fillComponents(holder);			
		row.setTag(holder);
		return row;
	}		
	
	public void setComponents(CartaHolder holder, View row){
		holder.pergunta = (TextView) row.findViewById(R.id.txvPerguntaList);		
		holder.editar = (ImageButton) row.findViewById(R.id.btnEditarCarta);
		holder.cancelar = (ImageButton) row.findViewById(R.id.btnCancelarCarta);
		holder.excluir = (ImageButton) row.findViewById(R.id.btnExcluirCarta);
		
	}
	
	public void fillComponents(CartaHolder holder){
		holder.pergunta.setText(holder.carta.getPergunta());
		holder.editar.setTag(holder);
		holder.excluir.setTag(holder);
		holder.cancelar.setTag(holder);
	}
	
	public void startEdit(CartaHolder holder){
		holder.pergunta.setTextColor(Color.GREEN);
		holder.editar.setVisibility(View.GONE);
		holder.excluir.setVisibility(View.INVISIBLE);
		holder.cancelar.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void add(Carta object) {		
		super.add(object);
		super.notifyDataSetChanged();
	}
	
	public Carta getEditCarta() {
		return editHolder.carta;
	}

	public void setEditHolder(CartaHolder holder) {
		this.editHolder = holder;
		notifyDataSetChanged();
	}
	
	public boolean isEdit(){
		return editHolder != null;
	}
	
	public static class CartaHolder{
		private Carta carta;
		private TextView pergunta;
		private ImageButton editar;
		private ImageButton cancelar;
		private ImageButton excluir;
		

		public String getPergunta(){
			return carta.getPergunta();
		}
		public String getResposta(){
			return carta.getResposta();
		}
		
		public Carta getCarta(){
			return carta;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((carta == null) ? 0 : carta.hashCode());
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			CartaHolder other = (CartaHolder) obj;
			if (carta == null) {
				if (other.carta != null)
					return false;
			} else if (!carta.equals(other.carta))
				return false;
			return true;
		}
		
		
		
		
	}
}
